﻿using System.Activities.Presentation;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using Headsight.BPMN2WF.BPMN;
using Headsight.BPMN2WF.BPMN.Schemas;
using Headsight.BPMN2WF.Properties;
using Headsight.BPMN2WF.Transformation.BPMN2TransformerMappings;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Headsight.BPMN2WF.Manual
{
    [TestClass]
    public class ManualTest
    {
        #region private helper
        private string ReadFileContent(string fileName)
        {
            string content;
            using (var streamReader = File.OpenText(fileName))
                content = streamReader.ReadToEnd();
            return content;
        }

        private tDefinitions ToTDefinitions(string fileContent)
        {
            var encoding = new ASCIIEncoding();
            var bytes = encoding.GetBytes(fileContent);

            var ser = new XmlSerializer(typeof(tDefinitions));
            return (tDefinitions)ser.Deserialize(new MemoryStream(bytes));
        }

        private void Transform(tDefinitions tDef, string destinationFilename)
        {
            var flowNodesBlock = new BPMNFlowNodesBlockFactory().CreateBPMNFlowNodesBlock(tDef);
            var result = _sut.Transform(flowNodesBlock);
            var wfDesigner = (WorkflowDesigner)result.Model;
            wfDesigner.Save(destinationFilename);
        }
        #endregion

        private IBPMN2WF _sut;

        [TestInitialize]
        public void SetUp()
        {
            _sut = BPMN2WFFactory.Create(new BPMN2WFTransformationVNext(), true, null);
        }

        [TestMethod]
        [Ignore]
        public void TestExample1()
        {
            var content = ReadFileContent(Settings.Default.TestingRootDir + @"\Headsight.BPMN2WFTest\Manual\Example1.bpmn");
            var tDef = ToTDefinitions(content);
            var fileName = Settings.Default.TestingRootDir + @"\Headsight.BPMN2WFTest\Manual\Example1.xaml";
            Transform(tDef, fileName);
        }
    }
}
